import {
	getSetVirtualGroupList,
	editSetVirtualGroupInfo,
	deleteSetSkillGroup,
	deleteSetSkillGroupUsers,
	saveSkillGroupInfoUsers,
	getSetGroupUserList,
	getUserList,
} from "@/services/singleSkillSet";

export default {
	namespace: "singleSkillSet",
	state: {
		total: 0,
		loading: true,
		visible: false,
		data: [],
		isShowAddGroupMember: false,
		groups: [],
		tableLoading: false,
		group: {},
		userList: [],
		userListTotal: 0,
		userListLoading: false,
	},
	effects: {
		*getGroupList({ payload, callback }, { call, put, select }) {
			yield put({
				type: "changeLoading",
				payload: false,
			});
			const response = yield call(getSetVirtualGroupList, payload);
			if (callback) {
				callback(response);
			}
			const group = yield select((state) => state.singleSkillSet.group);

			yield put({
				type: "save",
				payload: {
					groups: response,
					loading: false,
					group: group.id ? group : response.length ? response[0] : {},
				},
			});
			if (response.length) {
				yield put({
					type: "loadGroup",
					payload: group.id ? group : response[0],
				});
			}

			yield put({
				type: "changeLoading",
				payload: false,
			});
		},
		*showAddGroup({ payload }, { call, put }) {
			yield put({
				type: "save",
				payload: {
					...payload,
				},
			});
		},
		*addGroup({ payload, callback }, { call, put }) {
			const response = yield call(editSetVirtualGroupInfo, payload);

			if (callback) {
				callback(response);
			}

			yield put({
				type: "getGroupList",
			});
		},
		*deleteGroup({ payload, callback }, { call, put }) {
			const response = yield call(deleteSetSkillGroup, payload);
			if (callback) {
				callback(response);
			}
			yield put({
				type: "save",
				payload: {
					group: {},
				},
			});
			yield put({
				type: "getGroupList",
			});
		},
		*loadUserList({ payload }, { call, put }) {
			yield put({
				type: "save",
				payload: {
					userListLoading: true,
				},
			});
			const response = yield call(getUserList, payload);
			yield put({
				type: "save",
				payload: {
					userList: response.records,
					userListTotal: response.total,
					userListLoading: false,
				},
			});
		},
		*saveGroup({ payload }, { call, put }) {
			yield put({
				type: "save",
				payload: {
					group: payload,
				},
			});

			yield put({
				type: "loadGroup",
				payload: {
					id: payload.id,
					tenantId: payload.tenantId,
					name: "",
					current: 1,
					size: 10,
				},
			});
		},
		*loadGroup({ payload }, { call, put }) {
			yield put({
				type: "save",
				payload: {
					tableLoading: true,
				},
			});

			const response = yield call(getSetGroupUserList, {
				id: payload.id,
				tenantId: payload.tenantId,
				name: payload.name ? payload.name : "",
				current: payload.current ? payload.current : 1,
				size: payload.size ? payload.size : 10,
			});
			if (response.code == -1) {
				return false;
			}
			yield put({
				type: "save",
				payload: {
					data: response.records,
					tableLoading: false,
					total: response.total,
				},
			});
		},
		*addGroupMember({ payload, callback }, { call, put }) {
			const response = yield call(saveSkillGroupInfoUsers, payload);
			if (callback) {
				callback();
			}
			yield put({
				type: "getGroupList",
			});
		},
		*showGroupMember({ payload }, { call, put }) {
			yield put({
				type: "save",
				payload: {
					...payload,
					userListLoading: true,
				},
			});

			if (payload.isShowAddGroupMember) {
				yield put({
					type: "loadUserList",
					payload: {
						id: payload.id,
						ctsNameOrCtsNo: "",
						current: 1,
						size: 10,
					},
				});
			}
		},
		*deleteUser({ payload, callback }, { call, put }) {
			const response = yield call(deleteSetSkillGroupUsers, payload);
			if (callback) {
				callback();
			}
		},
	},

	reducers: {
		save(state, { payload }) {
			return {
				...state,
				...payload,
			};
		},
		show(state, { payload }) {
			return {
				...state,
				visible: payload,
			};
		},
		showAddGroupMember(state, { payload }) {
			return {
				...state,
				isShowAddGroupMember: payload,
			};
		},
	},
};
